//
//  GPAFNetworking3.m
//  ios_yuece
//
//  Created by GuoPeng on 16/3/16.
//  Copyright © 2016年 GuoPeng. All rights reserved.
//

#import "GPAFNetworking3.h"
#import <AFNetworking.h>


@implementation GPAFNetworking3


//- (void)test {
//    my_struct str = (my_struct)(malloc(sizeof(my_struct)));
//    str -> a = 1;
//    str -> b = 2;
////    HYBObject *obj = [[HYBObject alloc] init];
////    self = [[self alloc ]init];
//    self.arg1 = @""
//}

- (void)dealloc
{

}

+ (void)get:(nonnull NSString *)urlString progress:(nullable void(^)(NSProgress * _Nonnull progress))progressBlock success:(nullable void(^)(id _Nonnull responseObject))successBlock failure:(nullable void(^)(NSString  * _Nonnull errorLD))failureBlokc
{
    
    NSParameterAssert(urlString);
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString* mutableURLString = [NSString stringWithFormat:@"%@%@",NetworkServer,urlString];
    __weak typeof(self) weakSelf = self;

    [session GET:mutableURLString parameters:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        __strong typeof(self) strongSelf = weakSelf;
        successBlock([strongSelf transformation:responseObject]);
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        failureBlokc(error.localizedDescription);
    }];
}

+ (NSDictionary *)transformation:(id)responseObject
{
    NSString *resultString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
#if DEBUG
    NSLog(@"**********************");
    NSLog(@"%@",resultString);
    NSLog(@"**********************");
#else

#endif
   
    NSData *jsonData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    
    
//    @try {
//        [dic objectForKey:@"resultCode"];
//    } @catch (NSException *exception) {
//        [dic setObject:@"0" forKey:@"resultCode"];
//    } @finally {
//        ReLogin
//    }
    
    return dic;
}



+ (void)post:(nonnull NSString *)urlString parameters:(nullable NSDictionary *)parameters progress:(nullable void(^)(NSProgress * _Nullable progress))progressBlock success:(void(^)(id _Nullable responseObject))successBlock failure:(void(^)(NSString  * _Nonnull errorLD))failureBlokc
{
//    [self dictionaryToJson:parameters];
    NSParameterAssert(urlString);
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString* mutableURLString = [NSString stringWithFormat:@"%@%@",NetworkServer,urlString];
    
//    [session.requestSerializer setValue:GETTOKEN forHTTPHeaderField:@"appUserToken"];
    __weak typeof(self) weakSelf = self;
    [session POST:mutableURLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        successBlock([weakSelf transformation:responseObject]);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlokc(error.localizedDescription);
    }];
}

+ (NSString *)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];

    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return str;
}




@end
